package com.um.jdy.platform.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.platform.entity.dto.NotificationDTO;
import com.um.jdy.common.platform.entity.po.Notification;
import com.um.jdy.common.platform.entity.vo.NotificationAdminVO;
import com.um.jdy.common.platform.entity.vo.NotificationVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.jdy.common.platform.service.NotificationService;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 管理类：平台通知推送设置
 */
@Component
public class NotificationManager extends BaseManager<NotificationService> {

    /**
     * 详情
     *
     * @return
     */
    public Response<NotificationVO> get() {
        Notification notification = service.getEntity("com_id", getComId());
        if(notification == null){
            notification = new Notification();
            notification.setComId(getComId());
            notification.setIsMpEnable(YesOrNo.No.name());
            notification.setIsOrderEnable(YesOrNo.No.name());
            service.save(notification);
        }
        NotificationVO notificationVO = UMBeanUtils.copyBean(notification,NotificationVO.class);
        List<NotificationAdminVO> adminList = JSONUtil.parseArray(notification.getAdminIds()).toList(NotificationAdminVO.class);
        notificationVO.setAdminList(adminList);
        return Response.success(notificationVO);
    }

    /**
     * 更新
     *
     * @param id
     * @param notificationDTO
     * @return
     */
    public Response update(String id, NotificationDTO notificationDTO) {
        Notification notification = BeanUtil.copyProperties(notificationDTO, Notification.class);
        notification.setPlatformNotificationId(id);
        notification.setAdminIds(JSONUtil.toJsonStr(notificationDTO.getAdminList()));
        service.updateEntity(notification);
        return Response.success();
    }

}
